package unused;




import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.python.core.PyObject;
import org.python.util.PythonInterpreter;




import com.hogofwar.DJbot.BasePlugin;
import com.hogofwar.DJbot.IRCBase;

public class COM_py implements BasePlugin {

	public void start(final IRCBase irc, final String[] extra, String channel,
			String sender, String hostname, boolean usename) {
	//System.out.println(extra[0]);
	//System.out.println(extra[1]);
	
	String start = "";
	if(usename == true){
		start = sender +": ";
	}
	
	if(extra.length != 0){
		if(extra[0] !=" "){
		String tosend = null;
		System.out.println("got this far....");
		 PythonInterpreter interp = new PythonInterpreter();
		String python = null;
		for(String s: extra){
			if(python == null){
			python = s;
			}else{
				python = python+" "+s;
			}
		}
		// System.setProperty("java.security.policy","test.policy");
		
		//  SecurityManager sm = new SecurityManager();
		 
		  //  System.setSecurityManager(sm);
		    
		//interp.exec(" import sys");
		Writer out = new StringWriter(); 
		interp.setOut(out); 
		interp.exec(python); 
		String result = out.toString();
			
			//interp.exec("sys.stdout = StringIO()");
			//interp.exec(python);
			//interp.exec("sys.stdout.seek(0); outputtext = sys.stdout.read();");
			//PyObject x = interp.get("outputtext");
		
			// TODO Auto-generated catch block
			
			 tosend = result;
		
		   //
		// tosend =  " seems to be up.";

	    

	
irc.sendMessage(channel,start+""+tosend);
	}else{
		
		irc.sendMessage(channel,start+"You need to specify an input!");
	}
	}else{
	
		irc.sendMessage(channel,start+"You need to specify an input!");
	}
}


public String help() {
	return "Py - py [code] - Executes python.";
	// TODO Auto-generated method stub
	
}
//public static boolean exists(String URLName){
//    try {
//      HttpURLConnection.setFollowRedirects(true);
//      // note : you may also need
//              //HttpURLConnection.setInstanceFollowRedirects(false)
//      HttpURLConnection con =
//         (HttpURLConnection) new URL(URLName).openConnection();
//      con.setRequestMethod("HEAD");
//      return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
//    }
//    catch (Exception e) {
//       e.printStackTrace();
//       return false;
//    }
//  }
public String version() {
	return "Py version 1";
	// TODO Auto-generated method stub
	
}


@Override
public String restrict() {
	// TODO Auto-generated method stub
	return null;
}


@Override
public Boolean hide() {
	// TODO Auto-generated method stub
	return null;
}


@Override
public String aliases() {
	// TODO Auto-generated method stub
	return null;
}


}
